

package com.bei_en.teacher.admin.service.article;

import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bei_en.teacher.admin.service.BaseService;
import com.bei_en.teacher.admin.model.article.bean.ArticleType;
import com.bei_en.teacher.admin.model.article.vo.ArticleTypeVo;
import com.bei_en.teacher.admin.model.article.condition.ArticleTypeCondition;
import com.bei_en.teacher.admin.persistence.dao.article.ArticleTypeDao;

/**
 * 
 * @author Guocg
 * @date 2016-12-5 22:20:02
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class ArticleTypeService extends BaseService{

    @Autowired
    private ArticleTypeDao articleTypeDao;

    public void insert(ArticleTypeVo model) {

    	 articleTypeDao.insert(model.getArticleType());
    }

//    /**
//     * 插入并返回生成的id
//     * @param model
//     * @return
//     */
//    public Integer merge(ArticleTypeVo model) {
//
//	articleTypeDao.insert(model.getArticleType());
//	return articleTypeDao.findLastInsertedId();
//
//    }

    public void delete(Integer modelInteger) {

    	articleTypeDao.delete(modelInteger);
    }

    public ArticleTypeVo findById(Integer modelInteger) {

    	ArticleTypeVo model = new ArticleTypeVo(articleTypeDao.findById(modelInteger));
    	return model;
    }

    public void update(ArticleTypeVo model) {

    	articleTypeDao.update(model.getArticleType());
    }

    /**
     * 动态更新,只更新非null的值
     * @param model
     */
    public void updateSelective(ArticleTypeVo model) {

    	articleTypeDao.updateSelective(model.getArticleType());
    }

    public int countAll() {

    	return articleTypeDao.countAll();
    }

    public List<ArticleTypeVo> findAll() {

		List<ArticleTypeVo> list = new ArrayList<ArticleTypeVo>();
		List<ArticleType> list1 = articleTypeDao.findAll();
		for (ArticleType model : list1) {
		    list.add(new ArticleTypeVo(model));
		}
		return list;
    }

    public List<Integer> findAllIds() {

    	return articleTypeDao.findAllIds();
    }

    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<ArticleTypeVo> findArticleTypePage(ArticleTypeCondition vo) {

		Integer totalCount = searchArticleTypeNum(vo);
		List<ArticleTypeVo> list = new ArrayList<ArticleTypeVo>();
		vo.setTotalCount(totalCount);
	
		if (totalCount>0) {
		    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "id");
		    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");
	
		    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		    List<ArticleType> list1 = articleTypeDao.findPageBreakByCondition(vo, rb);
		    for (ArticleType model : list1) {
		    	list.add(new ArticleTypeVo(model));
		    }
	
		}

		return list;
    }

    private Integer searchArticleTypeNum(ArticleTypeCondition Vo) {

    	return articleTypeDao.findNumberByCondition(Vo);
    }

}
